package com.xiaozhen.jt809.handler.server;

import com.alibaba.fastjson.JSONObject;
import com.xiaozhen.jt809.constant.JT809BusinessConstant;
import com.xiaozhen.jt809.domain.JT809MessageInfo;
import com.xiaozhen.jt809.domain.UpExgMsg;
import com.xiaozhen.jt809.domain.UpWarnMsg;
import com.xiaozhen.jt809.handler.MyHandler;
import com.xiaozhen.jt809.utils.ByteArrayUtil;
import com.xiaozhen.jt809.utils.DateUtil;
import io.netty.channel.ChannelHandlerContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.stream.function.StreamBridge;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

/**
 * HISTORY
 *
 * @author zhenweisheng
 * @date 2021/9/17 14:28
 * @description:
 */
@Slf4j
@Service
public class UpWarnHandler implements MyHandler {

    @Autowired
    private StreamBridge streamBridge;

    @Override
    public void handler(ChannelHandlerContext ctx, JT809MessageInfo messageInfo) {
        log.info("接收到主链路报警信息");
        byte[] messageBody = messageInfo.getMessageBody();
        int index = 0;
        String vehicleNo = ByteArrayUtil.bytes2gbkString(ByteArrayUtil.subBytes(messageBody, index, 21));
        vehicleNo = vehicleNo.replaceAll("\\u0000", "");
        index += 21;
        int vehicleColor = ByteArrayUtil.bytes2int(ByteArrayUtil.subBytes(messageBody, index, 1));
        index += 1;
        int dataType = ByteArrayUtil.bytes2int(ByteArrayUtil.subBytes(messageBody, index, 2));
        index += 2;
        int dataLen = ByteArrayUtil.bytes2int(ByteArrayUtil.subBytes(messageBody, index, 4));
        index += 4;
        byte[] data = ByteArrayUtil.subBytes(messageBody, index, dataLen);
        index += dataLen;
        UpWarnMsg upWarnMsg = new UpWarnMsg();
        upWarnMsg.setVehicleNo(vehicleNo);
        upWarnMsg.setVehicleColor(vehicleColor);
        upWarnMsg.setDataType(dataType);
        upWarnMsg.setDataLen(dataLen);
        upWarnMsg.setData(data);
        byte[] dataTypeBytes = ByteArrayUtil.int2bytes(dataType);
        log.info("子业务类型：" + ByteArrayUtil.bytes2HexStr(ByteArrayUtil.subBytes(dataTypeBytes, 2, 2)));
        switch (dataType) {
            case JT809BusinessConstant.UpWarnMsg.UP_WARN_MSG_URGE_TODO_ACK:
                upWarnMsgUrgeTodoAck(upWarnMsg);
                break;
            case JT809BusinessConstant.UpWarnMsg.UP_WARN_MSG_ADPT_INFO:
                upWarnMsgAdptInfo(upWarnMsg);
                break;
            default:
                log.info("主链路报警信息交互。。。。。正在紧急开发中");
        }
    }

    /**
     * 报警督办应答消息
     */
    public void upWarnMsgUrgeTodoAck(UpWarnMsg upWarnMsg) {
        byte[] data = upWarnMsg.getData();
        int index = 0;
        int superVersionId = ByteArrayUtil.bytes2int(ByteArrayUtil.subBytes(data, index, 4));
        index += 4;
        int result = ByteArrayUtil.bytes2int(ByteArrayUtil.subBytes(data, index, 1));
        UpWarnMsg.UpWarnMsgUrgeTodoAck upWarnMsgUrgeTodoAck = new UpWarnMsg.UpWarnMsgUrgeTodoAck();
        upWarnMsgUrgeTodoAck.setSuperVersionId(superVersionId);
        upWarnMsgUrgeTodoAck.setResult(result);
        upWarnMsg.setUpWarnMsgUrgeTodoAck(upWarnMsgUrgeTodoAck);
        log.info("报警督办应答消息:" + JSONObject.toJSONString(upWarnMsg));
        sendUpWarnMsg(upWarnMsg);
    }

    /**
     * 上报报警信息处理
     */
    public void upWarnMsgAdptInfo(UpWarnMsg upWarnMsg) {
        byte[] data = upWarnMsg.getData();
        int index = 0;
        int warnSrc = ByteArrayUtil.bytes2int(ByteArrayUtil.subBytes(data, index, 1));
        index += 1;
        int warnType = ByteArrayUtil.bytes2int(ByteArrayUtil.subBytes(data, index, 2));
        index += 2;
        byte[] warnTime = ByteArrayUtil.subBytes(data, index, 8);
        index += 8;
        int infoId = ByteArrayUtil.bytes2int(ByteArrayUtil.subBytes(data, index, 4));
        index += 4;
        int infoLength = ByteArrayUtil.bytes2int(ByteArrayUtil.subBytes(data, index, 4));
        index += 4;
        String infoContent = ByteArrayUtil.bytes2gbkString(ByteArrayUtil.subBytes(data, index, infoLength));
        UpWarnMsg.UpWarnMsgAdptInfo upWarnMsgAdptInfo = new UpWarnMsg.UpWarnMsgAdptInfo();
        upWarnMsgAdptInfo.setWarnSrc(warnSrc);
        upWarnMsgAdptInfo.setWarnType(warnType);
        upWarnMsgAdptInfo.setWarnTime(DateUtil.stampToTime(ByteArrayUtil.bytes2int(warnTime)));
        upWarnMsgAdptInfo.setInfoId(infoId);
        upWarnMsgAdptInfo.setInfoLength(infoLength);
        upWarnMsgAdptInfo.setInfoContent(infoContent);
        upWarnMsg.setUpWarnMsgAdptInfo(upWarnMsgAdptInfo);
        sendUpWarnMsg(upWarnMsg);
        log.info("上报报警信息处理:" + JSONObject.toJSONString(upWarnMsg));
    }

    @Async
    public void sendUpWarnMsg(UpWarnMsg upWarnMsg){
        streamBridge.send("upWarn-out-0",upWarnMsg);
    }
}
